package ltd.dujiabao.activemq.springboot.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.TextMessage;

/**
 * @author : DuJiabao
 * @Project : active-mq
 * @Time : 2021/5/6 12:48
 * @Desc :
 */
@Slf4j
@Component
public class ActiveMQConsumer {
  ///**
  // * 写了一个TextMessage监听器
  // *
  // * @param textMessage
  // * @throws JMSException
  // */
  //@JmsListener(destination = "${myqueue}")
  //public void receiveText(TextMessage textMessage) throws JMSException {
  //  String text = textMessage.getText();
  //  log.info("接收到TextMessage信息：" + text);
  //}

  @JmsListener(destination = "${myqueue}")
  public void receiveText(Message message) throws JMSException {
    if (message instanceof TextMessage) {
      TextMessage textMessage = (TextMessage) message;
      log.info("接收到一个TextMessage：" + textMessage.toString());
    }

    if (message instanceof MapMessage) {
      MapMessage mapMessage = (MapMessage) message;
      log.info("接收到一个MapMessage：" + mapMessage.toString());
    }
  }

  @JmsListener(destination = "${clocktopic}")
  public void receiveTime(TextMessage textMessage) throws JMSException {
    log.info("当前时间是:：" + textMessage.getText());
  }
}
